$include("$theme/popup.css");

$import("fp.jq.Scrollable");

with ($ns("fp.controls"))
{
    fp.controls.SubscribePopup = function($p_container, p_options)
    {
        var me = new mx.controls.JQControl($p_container, p_options);

        
        me.folders = [];
        me.allItems = [];
        
        
        var _$body = null;
        
        
        me.init = function()
        {            
            me.$container.hide();
            me.$container.find("li").live("click", _item_click);
        
            
            fp.AppMain.feedViewContainer.bind("reloading", _feedViewContainer_reloading);
            $("body > #main > #top > #path").click(_path_click);
            
            
            _$body = me.$container.find("#body");
            _$body.verticalScrollable({ height: 420, margin:15, marginTop:5 });
            
            me.$container.find("#close").click(me.hide);
            
            me.visible = false;
        };
        
        
        
        me.load = function()
        {
            g.Reader.getSubscriptions({
                success: _getSubscriptions_callback
            });
        };
        
        
        
        me.show = function()
        {
            me.visible = true;
            me.$container.fadeIn("fast", function(){
                _$body.updateScrollbars();
            });
            
            me.$container.find("li.selected").scrollIntoView();
            
            $(document.body).bind("mouseup", _document_click);
        };
        
        me.hide = function()
        {
            me.visible = false;
            me.$container.hide("fast");
            $(document.body).unbind("mouseup", _document_click);
        };
        
        
        
        
        function _getSubscriptions_callback(p_result)
        {
            var subscriptions = p_result.subscriptions;
            var rootFolder = { id: "root", subscribes: [] };
            var folders = [ rootFolder ];
            
            for (var i = 0; i < subscriptions.length; i++)
            {
                var sub = subscriptions[i];
                var category = sub.categories[0];
                var folder = null;
                if (category != null)
                {
                    if (folders[category.id] == null)
                    {
                        folder = { id: category.id, title: category.label, sortID: category.sortid, subscribes: [] };
                        folders[category.id] = folder;
                        folders.add(folder);
                        me.allItems[folder.id] = folder;
                        me.allItems.add(folder);
                    }
                    else
                    {
                        folder = folders[category.id];
                    }
                }
                else
                {
                    folder = rootFolder;
                }
                
                var s = {
                    id: sub.id,
                    title: sub.title,
                    url: sub.htmlUrl,
                };
                folder.subscribes.add(s);
                me.allItems.add(s);
                me.allItems[s.id] = s;
            }

            
            var $ul = me.$container.find("ul");
            for (var i = 0; i < folders.length; i++)
            {
                var folder = folders[i];
                if (folder.subscribes.length > 0)
                {
                    var $folder = $("<li class='folder'><a/></li>");
                    $folder.attr("id", folder.id);
                    $folder.find("a").text(folder.title);
                    if (i > 0)
                    {
                        $ul.append($folder);
                    }
                    for (var j = 0; j < folder.subscribes.length; j++)
                    {
                        var sub = folder.subscribes[j];
                        var $sub = $("<li><a/></li>");
                        $sub.attr("id", sub.id);
                        if (i > 0)
                        {
                            $sub.addClass("subItem");
                        }
                        $sub.find("a").text(sub.title);
                        $sub.css("title", sub.title);
                        var $link = $sub.find("a");
                        $link.css("backgroundImage", "url(" + $mappath("$google/s2/favicons?domain_url=") + sub.url + ")");
                        $ul.append($sub);
                    }
                }
            }
            
            
            me.folders = folders;
            
            _$body.updateScrollbars();
        }
        
        function _feedViewContainer_reloading(e)
        {
            var path = e.target.path;
            var title = null;
            if (path == "user/-/state/com.google/starred")
            {
                title = $msg("fp", "STARRED_ITEMS");
            }
            else if (path == "user/-/state/com.google/reading-list")
            {
                title = $msg("fp", "ALL_ITEMS");
            }
            else if (path == "UNREAD")
            {
                title = $msg("fp", "UNREAD_ITEMS");
            }
            else if (me.allItems[path])
            {
                var item = me.allItems[path];
                title = item.title;
            }
            $("body > #main > #top > #path > #title").text(title).attr("title", title);
            
            var $li = me.$container.find("li[id='" + path + "']");
            me.$container.find("li.selected").removeClass("selected");
            $li.addClass("selected");
        }
        
        function _item_click(e)
        {
            fp.AppMain.load(this.id);
        }
        
        function _path_click(e)
        {
            if (me.visible)
            {
                me.hide();
            }
            else
            {
                me.show();
            }
        }
        
        function _document_click(e)
        { 
            if (_$body.get(0).contains(e.target) || $("body > #main > #top > #path").get(0).contains(e.target))
            {
                return;
            }
            
            e.preventDefault();
            
            
            me.hide();
        }
        
        return me;
    };
}